import { CacheTypeEnum, LOGIN_INFO_KEY, TENANT_ID, TOKEN_KEY } from '@/enums/cacheEnum';
import projectSetting from '@/settings/projectSetting';
import { locStorage, sesStorage } from '../storage';

const { permissionCacheType } = projectSetting;
const isLocal = permissionCacheType === CacheTypeEnum.LOCAL;

/**
 * 获取token
 */
export function getToken() {
  return getAuthCache<string>(TOKEN_KEY);
}
/**
 * 获取登录信息
 */
export function getLoginBackInfo() {
  return getAuthCache(LOGIN_INFO_KEY);
}
/**
 * 获取租户id
 */
export function getTenantId() {
  return getAuthCache<string>(TENANT_ID);
}

export function getAuthCache<T>(key: string) {
  const fn = isLocal ? locStorage.getItem : sesStorage.getItem;
  return fn(key) as T;
}

export function setAuthCache(key: string, value:any) {
  const fn = isLocal ? locStorage.setItem : sesStorage.setItem;
  return fn(key, value);
}

/**
 * 移除缓存中的某个属性
 * @param key
 * @update:移除缓存中的某个属性
 * @updateBy: sdw
 * @updateDate:2022-09-07
 */
export function removeAuthCache<T>(key: string) {
  const fn = isLocal ? locStorage.removeItem : sesStorage.removeItem;
  return fn(key);
}

export function clearAuthCache() {
  const fn = isLocal ? locStorage.clear : sesStorage.clear;
  return fn();
}
